using System;
using System.Collections.Generic;
using System.Text;

namespace hlavny.modul1
{
    [Serializable]
    public class LongInt : Object
    {
        #region Constants
        private const int NUMBER_OF_BYTES_FOR_LONGINT = 8;
        #endregion

        #region Fields
        private Int64? _value;
        #endregion

        #region Constructors
        public LongInt(Int64? value)
        { 
            _value = value;
        }

        public LongInt(byte[] value)
        {
            FromByte(value);
        }

        public LongInt(string value)
        {
            _value = Convert.ToInt64(value);
        }

        public LongInt(object value)
        {
            _value = Convert.ToInt64(value);
        }

        #endregion

        #region Public Methods
        /// <summary>
        /// Converts current value to array of bytes.
        /// </summary>
        /// <returns></returns>
        public override byte[] ToByte()
        {
            byte[] buffer = new byte[NUMBER_OF_BYTES_FOR_LONGINT + 1];

            if (!this.IsNull())
            {
                byte[] tmp = BitConverter.GetBytes((Int64)_value);
                buffer[0] = 1;

                for (int i = 0; i < tmp.Length; i++)
                {
                    buffer[i + 1] = tmp[i];
                }
            }
            else
            {
                for (int i = 0; i <= NUMBER_OF_BYTES_FOR_LONGINT; i++)
                {
                    buffer[i] = 0;
                }
            }

            return buffer;
        }

        /// <summary>
        /// Converts the array of bytes to LongInt value.
        /// </summary>
        /// <param name="buffer"></param>
        public override void FromByte(byte[] buffer)
        {
            if (buffer.Length < NUMBER_OF_BYTES_FOR_LONGINT + 1)
                throw new ApplicationException("Cannot convert to LongInt because of too short array of bytes");

            if (buffer[0] == 1)
                this._value = BitConverter.ToInt64(buffer, 1);
            else if (buffer[0] == 0)
                this._value = null;
            else
                throw new ApplicationException("Invalid first byte format in array of bytes");
        }

        /// <summary>
        /// If current value is null then return True, else return False.
        /// </summary>
        /// <returns></returns>
        public override bool IsNull()
        {
            if (_value.HasValue)
                return false;
            else
                return true;
        }

        /// <summary>
        /// Compare two instances of LongInt values.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public override int Compare(modul1.Object val)
        {
            LongInt value = val as LongInt;
            if (value.IsNull() && this.IsNull())
                return 0;
            else if (value.IsNull() && !this.IsNull())
                return 1;
            else if (!value.IsNull() && this.IsNull())
                return -1;
            else if (this._value > value._value)
                return 1;
            else if (this._value < value._value)
                return -1;
            else
                return 0;
        }

        /// <summary>
        /// If value is type of LongInt return True, else return False.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsTypeOf(Object value)
        {
            System.Type type = value.GetType();

            if (type.FullName == "Structures.LongInt")
                return true;
            else
                return false;
        }

        /// <summary>
        /// Get number of bytes needed to LongInt type.
        /// </summary>
        /// <returns></returns>
        public override int GetLength()
        {
            return NUMBER_OF_BYTES_FOR_LONGINT + 1;
        }

        /// <summary>
        /// Converts current value to string.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (!this.IsNull())
                return _value.ToString();
            else
                return string.Empty;
        }

        /// <summary>
        /// Converts current value to string.
        /// </summary>
        /// <returns></returns>
        public static bool IsTypeOf(string value)
        {
            Int64 tmp;
            return Int64.TryParse(value, out tmp);
        }
        #endregion
    }
}
